﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Common;
using Microsoft.TeamFoundation.VersionControl.Client;
using Microsoft.TeamFoundation.WorkItemTracking;
using Microsoft.TeamFoundation.WorkItemTracking.Client;
using Microsoft.TeamFoundation.WorkItemTracking.Controls;
using TfsBugger.Dialogs;
using TfsBugger.Forms;
using TfsBugger.Util.Ui;

namespace TfsBugger
{
    public partial class AppShell : Form
    {
        public AppShell()
        {
            InitializeComponent();
        }


        #region Event Handlers 
        private void OnFormShown(object sender, EventArgs e)
        {
            SetAvailUiCommands();
        }


        /// <summary>
        /// Handles <see cref="Program.TfsServerChanged" /> to update 
        /// available commands in the UI.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnTfsServerChanged(object sender, EventArgs e)
        {
            SetAvailUiCommands();
        }


        #region Toolbar Commands 
        private void OnConnectButtonClick(object sender, EventArgs e)
        {
            var dialog = new ServerConnectDialog();
            if (dialog.ShowDialog() != DialogResult.OK)
                return;

            this.btnCheckInBugger.Enabled = true;
            using (WaitScope wait = new WaitScope(this))
            {
                foreach (var proj in Program.SelectedProjects)
                {
                    var projNode = new TreeNode(proj.Name);
                    this.trvProjects.Nodes.Add(projNode);
                    var qryRootNode = new TreeNode("Work Items");
                    projNode.Nodes.Add(qryRootNode);
                    foreach (StoredQuery query in proj.StoredQueries)
                    {
                        var qryNode = new TreeNode(query.Name);
                        qryNode.Tag = query;
                        qryRootNode.Nodes.Add(qryNode);
                    }
                }
            }
        }


        private void OnCheckInBuggerBtnClick(object sender, EventArgs e)
        {
            CheckInBuggerForm frm = new CheckInBuggerForm();;
            frm.Show();
        } 
        #endregion


        private void OnNodeDblClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (null != e.Node.Tag)
            {
                using (WaitScope wait = new WaitScope(this))
                {
                    var qry = (StoredQuery)e.Node.Tag;
                    string wiql = qry.QueryText.ToLower();
                    if (wiql.Contains("@project"))
                        wiql = wiql.Replace("@project", string.Format("'{0}'", qry.Project.Name));

                    var results = qry.Project.Store.Query(wiql);
                    var grid = new WorkItemResultGrid();
                    grid.DoubleClick += OnWorkItemDoubleClick;
                    

                }
            }
        }


        private void OnWorkItemDoubleClick(object sender, EventArgs e)
        {

        }
        #endregion 


        #region Methods
        private void SetAvailUiCommands()
        {
            bool loaded = Program.IsTfsLoaded;
            this.btnCheckInBugger.Enabled = loaded;
        }
        #endregion
    }
}
